<?php

namespace App\Admin\Controllers\user;

use App\Admin\Controllers\user\actions\Copy;
use App\Admin\Controllers\user\actions\ImportUser;
use App\Admin\Controllers\User\actions\Push;
use App\Common\statement\CommonStatement;
use App\Exceptions\api\BaseException;
use App\Models\config\Config;
use App\Models\User;
use App\Models\user\Depart;
use App\Services\goods\GoodsService;
use App\Services\user\DepartService;
use Encore\Admin\Actions\Action;
use Encore\Admin\Actions\GridAction;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Tools;
use Encore\Admin\Show;

class UserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {


        $grid = new Grid(new User());

//        //去掉导出按钮
        $grid->disableExport();

//        禁用创建按钮
//        $grid->disableCreateButton();

        //编辑列表
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            //展开搜索
//            $filter->expand();
            $filter->column(1/3, function ($filter) {

//                $filter->equal( 'id',__('Id'));
                $filter->where(function ($query) {

                    $query->where('name', 'like', "%{$this->input}%");

                },__('Name'));

                $filter->like( 'phone',__('Phone'));

            });

            $filter->column(1/3, function ($filter) {
                $filter->equal('level','用户类型')->select(CommonStatement::level_text);
                $filter->between('created_at',__('Created at'))->datetime();
            });

        });
        $grid->model()->orderBy('id','desc');

        $grid->column('id', __('Id'))->sortable();


        $grid->column('header_img', __('header_img'))
            ->lightbox(['width' => 50, 'height' => 50, 'class' => ['circle', 'thumbnail']]);

        $grid->column('name', __('Name'));

        $grid->column('phone', __('Phone'))->copyable();//添加一个复制按钮

        $grid->column('depart_id', __('Depart'))->display(function ($depart_id){
            $depart_name = Depart::whereIn('id',explode(',',$depart_id))->pluck('name')->toArray();
            return implode(',',$depart_name);
        });
        $grid->column('duty', __('Duty'));
        $grid->column('money', __('Money'));
        $grid->column('level', __('Level'))->display(function ($s){
            return CommonStatement::level_text[$s];
        });

//        $grid->column('status', __('Status'))->switch(CommonStatement::status_switch);


        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new ImportUser());
        });

//        //添加推送按钮
//        $grid->actions(function ($actions) {
//            $actions->add(new Push());
//        });


        //根据条件显示隐藏行按钮
//        $grid->actions(function ($actions) {
//
//            if($actions->row->status  == 0)
//            {
//                $actions->add(new Copy());
//            }
//            $actions->disableDelete();
//            $actions->disableEdit();
//            $actions->disableView();
//        });

        $grid->column('created_at', __('Created at'))->sortable();
        $grid->column('updated_at', __('Updated at'))->sortable();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {

        $show = new Show(User::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('phone', __('Phone'));
        $show->field('money', __('Money'));
        $show->field('duty', __('Duty'));
        $show->field('cert_number', __('Cert Number'));
        $show->field('level', __('Level'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);

//        $form->display('id', __('ID'));
        $form->image('header_img',__('header_img'))
            ->default(config('app.default_header'));
        $form->number('money',__('Money'))->default(0);

        $form->text('name', __('Name'))->rules('required|min:2');

        $form->text('phone', __('Phone'));
//            ->creationRules('required|phone|unique:users')
//            ->updateRules('required|phone|unique:users,phone,{{id}}');

        $form->text('cert_number', __('Cert Number'));

        $form->radioCard('level',__('Level'))->options(CommonStatement::level_text)
            ->when('in',[1,3],function (Form $form){
                $form->radioCard('depart_id',__('Depart'))->options(DepartService::getBranch(1));
            })
            ->when('=',2,function (Form $form){
                $form->checkboxCard('depart_id',__('Depart'))->options(DepartService::getBranch(1));
            })->default(1);
        $form->select('branch_id',__('Branch'))->options(DepartService::getBranch(2))->rules('required');
        $form->text('duty',__('Duty'))->rules('required|min:2');
        $form->text('best_count',__('好评个数'))->default(10)->rules('required')->attribute("autocomplete","new-password");
        $form->text('best_dir_count',__('直属好评个数'))->default(10)->rules('required')->attribute("autocomplete","new-password");

        $form->password('password', __('Password'))->default(12345678)->attribute("autocomplete","new-password");
        $form->radio('sex',__('Sex'))
            ->options(CommonStatement::sex)
            ->default(CommonStatement::default_sex);

        //保存前回调
        $form->saving(function (Form $form) {

            if($form->model()->id){
                $user = User::where('id','neq',$form->id)->where('name',$form->name)->first();
            }else{
                $user = User::where('name',$form->name)->first();
            }
            if($user){
                throw new BaseException('导入失败:'.$user['name'].'-姓名已存在,不能重复添加');
            }

            if (!$form->password){
                $form->password = $form->model()->password;
            }
        });

        $form->saved(function (Form $form) {

        });

        return $form;
    }
}
